

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cheney">
  <meta name="keywords" content="Coding">
  
    <meta name="description" content="设计、编制并调试一个词法分析程序，加深对词法分析原理的理解。利用最小化DFA做词法分析，第二部分实现DFA。">
<meta property="og:type" content="article">
<meta property="og:title" content="基于DFA的词法分析（二）：构造DFA">
<meta property="og:url" content="https://cheney822.gitee.io/2021/04/20/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86__%E5%9F%BA%E4%BA%8EDFA%E7%9A%84%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90%EF%BC%88%E4%BA%8C%EF%BC%89%EF%BC%9A%E6%9E%84%E9%80%A0DFA/index.html">
<meta property="og:site_name" content="Cheney blog">
<meta property="og:description" content="设计、编制并调试一个词法分析程序，加深对词法分析原理的理解。利用最小化DFA做词法分析，第二部分实现DFA。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-04-20T00:22:00.000Z">
<meta property="article:modified_time" content="2022-04-05T13:14:27.451Z">
<meta property="article:author" content="Cheney">
<meta property="article:tag" content="C++">
<meta property="article:tag" content="编译原理">
<meta property="article:tag" content="DFA">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>基于DFA的词法分析（二）：构造DFA - Cheney blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"cheney822.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":1},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Cheney Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://imgbed.cheney.cc/Blog_config/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="基于DFA的词法分析（二）：构造DFA">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-04-20 08:22" pubdate>
        2021年4月20日 早上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      22k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      112 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">基于DFA的词法分析（二）：构造DFA</h1>
            
            <div class="markdown-body">
              <p>字符串预处理（加上&amp;符号分隔）<br>中缀转后缀（结果仍然是string）<br>后缀转NFA：</p>
<p>定义一个存NFA的栈<br>定义一个NFA　Ans用于保存最后的答案，下面提到的状态状态数等无特殊说明均指Ans的成员<br>一个循环：每次读取后缀表达式的一个字符，读完为止<br>然后根据读到的字符串的不同，可以分为以下几种情况处理：<br>对操作数的定义为：从正则表达式读取到的非操作符的字符，存在一个set（All_Char）内</p>
<ul>
<li>–操作数：<br>  新建一个NFA（新建两个节点即状态（状态总数＋２），然后NFA的头尾分别指向它们）<br>  修改关系：让ｎ的头部指向的节点的出弧上的值为读到的字符，转换到的终点为尾指针指向状态的编号<br>  ｎ进栈</li>
<li>–‘*’：<br>   新建一个NFA，N２（新建两个节点即状态（状态总数＋２），然后NFA的头尾分别指向它们）<br>  出栈一个NFA称为N１，ｎ１原有的结构保持不变<br>  连接以下内容：<div class="code-wrapper"><pre><code class="hljs">  N２的头连接N１的头，N１的尾连接N２的尾
  N２的头连接N２的尾，N１的尾连接N１的头
  以上连接均是由空字符连接，（可以通过Ｅ＿closure完成）
</code></pre></div>
  ｎ２进栈</li>
<li>–‘|’：<br>  出栈两个元素ａ，ｂ。<br>  新建一个NFA，N（新建两个节点即状态（状态总数＋２），然后NFA的头尾分别指向它们）<br>  做以下连接：<div class="code-wrapper"><pre><code class="hljs">  N的头连接到ａ和ｂ的头，ａ和ｂ的尾连接到N的尾（ａ，ｂ并联）
  以上连接均是由空字符连接，（可以通过Ｅ＿closure完成）    
</code></pre></div>
  N进栈</li>
<li>–‘&amp;’：<br>  出栈两个元素ａ，ｂ。<br>  ａ的尾通过空字符连接到ｂ的头（ａ，ｂ串联）<br>  然后将ａ头ｂ尾作为新NFA入栈即可</li>
</ul>
<p>做完以上操作，栈大小就变为１，取栈顶的头尾赋给答案Ａｎｓ再将其返回即可（不能直接返回栈顶因为其不含状态数等信息）</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br></pre></div></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">pragma</span> once</span><br><br><span class="hljs-comment">/*新建一个NFA（即从NFA状态数组中取出两个状态）*/</span><br><span class="hljs-function">NFA <span class="hljs-title">creatNFA</span><span class="hljs-params">(<span class="hljs-type">int</span> sum)</span></span><br><span class="hljs-function"></span>&#123;<br>	NFA New_N ;<br><br>	New_N.head = &amp;NFA_States[sum];<br>	New_N.tail = &amp;NFA_States[sum + <span class="hljs-number">1</span>];<br><br>	<span class="hljs-keyword">return</span> New_N;<br>&#125;<br><br><span class="hljs-comment">/*在字符串s第n位后面插入字符ch*/</span><br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">insert_behind_n</span><span class="hljs-params">(string&amp; s, <span class="hljs-type">int</span> n, <span class="hljs-type">char</span> ch)</span></span><br><span class="hljs-function"></span>&#123;<br><br>	s += <span class="hljs-string">&#x27;#&#x27;</span>;<br><br>	<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = s.<span class="hljs-built_in">size</span>() - <span class="hljs-number">1</span>; i &gt; n; i--)<br>		s[i] = s[i - <span class="hljs-number">1</span>];<span class="hljs-comment">//n后面的都向后挪一位</span><br><br>	s[n] = ch;<br>&#125;<br><br><span class="hljs-comment">/*对字符串s进行预处理，在第一位是操作数、‘*’或‘)’且第二位是操作数或‘(’之间加入连接符‘&amp;’*/</span><br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">pre_process</span><span class="hljs-params">(string&amp; s)</span></span><br><span class="hljs-function"></span>&#123;<br><br>	<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>, length = s.<span class="hljs-built_in">size</span>();<br><br>	<span class="hljs-keyword">while</span> (i &lt; length)<br>	&#123;<br>		<span class="hljs-type">int</span> in_all_1 = <span class="hljs-number">0</span>, in_all_2 = <span class="hljs-number">0</span>;<br>		set&lt;<span class="hljs-type">char</span>&gt;::iterator it;<br>		<span class="hljs-keyword">for</span> (it = All_Char.<span class="hljs-built_in">begin</span>(); it != All_Char.<span class="hljs-built_in">end</span>(); it++) &#123;<br>			<span class="hljs-keyword">if</span> (s[i] == *it)<br>				in_all_1 = <span class="hljs-number">1</span>;<br>			<span class="hljs-keyword">if</span> (s[i + <span class="hljs-number">1</span>] == *it)<br>				in_all_2 = <span class="hljs-number">1</span>;<br>		&#125;<br><br>		<span class="hljs-comment">//if ((s[i] &gt;= &#x27;a&#x27; &amp;&amp; s[i] &lt;= &#x27;z&#x27;) || (s[i] == &#x27;*&#x27;) || (s[i] == &#x27;)&#x27;))</span><br>		<span class="hljs-keyword">if</span> (in_all_1 || (s[i] == <span class="hljs-string">&#x27;*&#x27;</span>) || (s[i] == <span class="hljs-string">&#x27;)&#x27;</span>))<br>			<span class="hljs-keyword">if</span> (in_all_2 || s[i + <span class="hljs-number">1</span>] == <span class="hljs-string">&#x27;(&#x27;</span>)<br>			&#123;<br>				<span class="hljs-built_in">insert_behind_n</span>(s, i + <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;&amp;&#x27;</span>);<br>				length++;<br>			&#125;<br><br>		i++;<br>	&#125;<br>&#125;<br><br><span class="hljs-comment">/*中缀转后缀时用到的优先级比较，即为每个操作符赋一个权重，通过权重大小比较优先级*/</span><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">priority</span><span class="hljs-params">(<span class="hljs-type">char</span> ch)</span></span><br><span class="hljs-function"></span>&#123;<br><br>	<span class="hljs-keyword">if</span> (ch == <span class="hljs-string">&#x27;*&#x27;</span>)<br>	&#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">3</span>;<br>	&#125;<br><br>	<span class="hljs-keyword">if</span> (ch == <span class="hljs-string">&#x27;&amp;&#x27;</span>)<br>	&#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">2</span>;<br>	&#125;<br><br>	<span class="hljs-keyword">if</span> (ch == <span class="hljs-string">&#x27;|&#x27;</span>)<br>	&#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>	&#125;<br><br>	<span class="hljs-keyword">if</span> (ch == <span class="hljs-string">&#x27;(&#x27;</span>)<br>	&#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>	&#125;<br>&#125;<br><br><span class="hljs-comment">/*中缀表达式转后缀表达式*/</span><br><span class="hljs-function">string <span class="hljs-title">infixToSuffix</span><span class="hljs-params">(string s)</span></span><br><span class="hljs-function"></span>&#123;<br><br>	<span class="hljs-built_in">pre_process</span>(s);			<span class="hljs-comment">/*对字符串进行预处理*/</span><br><br>	string str;				<span class="hljs-comment">/*要输出的后缀字符串*/</span><br>	stack&lt;<span class="hljs-type">char</span>&gt; oper;		<span class="hljs-comment">/*运算符栈*/</span><br><br><br><br>	<span class="hljs-keyword">for</span> (<span class="hljs-type">unsigned</span> <span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; s.<span class="hljs-built_in">size</span>(); i++)<br>	&#123;<br>		<span class="hljs-type">int</span> in_all = <span class="hljs-number">0</span>;<br>		set&lt;<span class="hljs-type">char</span>&gt;::iterator it;<br>		<span class="hljs-keyword">for</span> (it = All_Char.<span class="hljs-built_in">begin</span>(); it != All_Char.<span class="hljs-built_in">end</span>(); it++)<br>			<span class="hljs-keyword">if</span> (s[i] == *it)<br>				in_all = <span class="hljs-number">1</span>;<br><br><br><br>		<span class="hljs-keyword">if</span> (in_all)	<span class="hljs-comment">/*如果是操作数直接输出*/</span><br>		&#123;<br>			str += s[i];<br>		&#125;<br>		<span class="hljs-keyword">else</span>							<span class="hljs-comment">/*遇到运算符时*/</span><br>		&#123;<br><br>			<span class="hljs-keyword">if</span> (s[i] == <span class="hljs-string">&#x27;(&#x27;</span>)			<span class="hljs-comment">/*遇到左括号压入栈中*/</span><br>			&#123;<br>				oper.<span class="hljs-built_in">push</span>(s[i]);<br>			&#125;<br><br>			<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (s[i] == <span class="hljs-string">&#x27;)&#x27;</span>)	<span class="hljs-comment">/*遇到右括号时*/</span><br>			&#123;<br><br>				<span class="hljs-type">char</span> ch = oper.<span class="hljs-built_in">top</span>();<br>				<span class="hljs-keyword">while</span> (ch != <span class="hljs-string">&#x27;(&#x27;</span>)		<span class="hljs-comment">/*将栈中元素出栈，直到栈顶为左括号*/</span><br>				&#123;<br><br>					str += ch;<br><br>					oper.<span class="hljs-built_in">pop</span>();<br>					ch = oper.<span class="hljs-built_in">top</span>();<br>				&#125;<br><br>				oper.<span class="hljs-built_in">pop</span>();				<span class="hljs-comment">/*最后将左括号出栈*/</span><br>			&#125;<br>			<span class="hljs-keyword">else</span>					<span class="hljs-comment">/*遇到其他操作符时*/</span><br>			&#123;<br><br>				<span class="hljs-keyword">if</span> (!oper.<span class="hljs-built_in">empty</span>())			<span class="hljs-comment">/*如果栈不为空*/</span><br>				&#123;<br><br>					<span class="hljs-type">char</span> ch = oper.<span class="hljs-built_in">top</span>();<br>					<span class="hljs-keyword">while</span> (<span class="hljs-built_in">priority</span>(ch) &gt;= <span class="hljs-built_in">priority</span>(s[i]))	<span class="hljs-comment">/*弹出栈中优先级大于等于当前运算符的运算符*/</span><br>					&#123;<br><br>						str += ch;<br>						oper.<span class="hljs-built_in">pop</span>();<br><br>						<span class="hljs-keyword">if</span> (oper.<span class="hljs-built_in">empty</span>())	<span class="hljs-comment">/*如果栈为空则结束循环*/</span><br>						&#123;<br>							<span class="hljs-keyword">break</span>;<br>						&#125;<br>						<span class="hljs-keyword">else</span> ch = oper.<span class="hljs-built_in">top</span>();<br>					&#125;<br><br>					oper.<span class="hljs-built_in">push</span>(s[i]);		<span class="hljs-comment">/*再将当前运算符入栈*/</span><br>				&#125;<br><br>				<span class="hljs-keyword">else</span>				<span class="hljs-comment">/*如果栈为空，直接将运算符入栈*/</span><br>				&#123;<br>					oper.<span class="hljs-built_in">push</span>(s[i]);<br>				&#125;<br>			&#125;<br>		&#125;<br>	&#125;<br><br>	<span class="hljs-comment">/*最后如果栈不为空，则出栈并输出到字符串*/</span><br>	<span class="hljs-keyword">while</span> (!oper.<span class="hljs-built_in">empty</span>())<br>	&#123;<br><br>		<span class="hljs-type">char</span> ch = oper.<span class="hljs-built_in">top</span>();<br>		oper.<span class="hljs-built_in">pop</span>();<br><br>		str += ch;<br>	&#125;<br><br>	cout &lt;&lt; <span class="hljs-string">&quot;*******************************************&quot;</span> &lt;&lt; endl &lt;&lt; endl;<br>	cout &lt;&lt; <span class="hljs-string">&quot;中缀表达式为：&quot;</span> &lt;&lt; s &lt;&lt; endl &lt;&lt; endl;<br>	cout &lt;&lt; <span class="hljs-string">&quot;后缀表达式为：&quot;</span> &lt;&lt; str &lt;&lt; endl &lt;&lt; endl;<br><br>	<span class="hljs-keyword">return</span> str;<br>&#125;<br><br><span class="hljs-comment">/*后缀表达式转nfa*/</span><br><span class="hljs-function">NFA <span class="hljs-title">str_To_Nfa</span><span class="hljs-params">(string s)</span></span><br><span class="hljs-function"></span>&#123;<br>	s = <span class="hljs-built_in">infixToSuffix</span>(s);		<span class="hljs-comment">/*将中缀表达式转换为后缀表达式*/</span><br><br>	stack&lt;NFA&gt; NfaStack;		<span class="hljs-comment">/*定义一个NFA栈*/</span><br>	NFA Ans;<br><br>	<span class="hljs-keyword">for</span> (<span class="hljs-type">unsigned</span> <span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; s.<span class="hljs-built_in">size</span>(); i++)		<span class="hljs-comment">/*读取后缀表达式，每次读一个字符*/</span><br>	&#123;<br>		<span class="hljs-comment">//if(i==83)	cout &lt;&lt; &quot;测试信息&quot; &lt;&lt; i &lt;&lt; endl;</span><br><br>		<span class="hljs-type">int</span> in_all = <span class="hljs-number">0</span>; <br>		set&lt;<span class="hljs-type">char</span>&gt;::iterator it;<br>		<span class="hljs-keyword">for</span> (it = All_Char.<span class="hljs-built_in">begin</span>(); it != All_Char.<span class="hljs-built_in">end</span>(); it++)<br>			<span class="hljs-keyword">if</span> (s[i] == *it)<br>				in_all = <span class="hljs-number">1</span>;<br>	<br>		<span class="hljs-keyword">if</span> (in_all)		<span class="hljs-comment">/*遇到操作数*/</span><br>		&#123;<br><br>			NFA n = <span class="hljs-built_in">creatNFA</span>(Ans.N_StateNum);		<span class="hljs-comment">/*新建一个NFA*/</span><br>			Ans.N_StateNum += <span class="hljs-number">2</span>;					<span class="hljs-comment">/*NFA状态总数加2*/</span><br><br>			<span class="hljs-comment">//add(n.head, n.tail, s[i]);			/*NFA的头指向尾，弧上的值为s[i]*/</span><br>			n.head-&gt;get_value = s[i]; <br>			n.head-&gt;Trans_to = n.tail-&gt;state_id;<br>			<br>	<span class="hljs-comment">/*		</span><br><span class="hljs-comment">			n.head-&gt;get_value = s[i];</span><br><span class="hljs-comment">			n.head-&gt;Trans_to = n.tail-&gt;state_id;*/</span><br><br><br>			NfaStack.<span class="hljs-built_in">push</span>(n);					<span class="hljs-comment">/*将该NFA入栈*/</span><br>		&#125;<br><br>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (s[i] == <span class="hljs-string">&#x27;*&#x27;</span>)		<span class="hljs-comment">/*遇到闭包运算符*/</span><br>		&#123;<br><br>			NFA n2 = <span class="hljs-built_in">creatNFA</span>(Ans.N_StateNum );		<span class="hljs-comment">/*新建一个NFA*/</span><br>			Ans.N_StateNum += <span class="hljs-number">2</span>;					<span class="hljs-comment">/*NFA状态总数加2*/</span><br><br>			NFA n1 = NfaStack.<span class="hljs-built_in">top</span>();			<span class="hljs-comment">/*从栈中弹出一个NFA*/</span><br>			NfaStack.<span class="hljs-built_in">pop</span>();<br><br>			<span class="hljs-comment">//add(n2.tail, n1.head);				/*n2的尾通过ε指向n1的头*/</span><br>			<span class="hljs-comment">//add(n2.tail, n1.tail);				/*n2的尾通过ε指向n1的尾*/</span><br>			<span class="hljs-comment">//add(n1.head, n2.head);				/*n1的头通过ε指向n2的头*/</span><br>			<span class="hljs-comment">//add(n1.head, n1.tail);				/*n1的头通过ε指向n1的尾*/</span><br><br>			n1.tail-&gt;E_closure_to.<span class="hljs-built_in">insert</span>(n2.head-&gt;state_id);				<span class="hljs-comment">/*n2的尾通过ε指向n1的头*/</span><br>			n1.tail-&gt;E_closure_to.<span class="hljs-built_in">insert</span>(n2.tail-&gt;state_id);				<span class="hljs-comment">/*n2的尾通过ε指向n1的尾*/</span><br>			n2.head-&gt;E_closure_to.<span class="hljs-built_in">insert</span>(n1.head-&gt;state_id);				<span class="hljs-comment">/*n1的头通过ε指向n2的头*/</span><br>			n2.head-&gt;E_closure_to.<span class="hljs-built_in">insert</span>(n2.tail-&gt;state_id);				<span class="hljs-comment">/*n1的头通过ε指向n1的尾*/</span><br><br>			NfaStack.<span class="hljs-built_in">push</span>(n2);					<span class="hljs-comment">/*最后将新生成的NFA入栈*/</span><br>		&#125;<br><br>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (s[i] == <span class="hljs-string">&#x27;|&#x27;</span>)		<span class="hljs-comment">/*遇到或运算符*/</span><br>		&#123;<br><br>			NFA n1, n2;							<span class="hljs-comment">/*从栈中弹出两个NFA，栈顶为n2，次栈顶为n1*/</span><br>			n2 = NfaStack.<span class="hljs-built_in">top</span>();<br>			NfaStack.<span class="hljs-built_in">pop</span>();<br><br>			n1 = NfaStack.<span class="hljs-built_in">top</span>();<br>			NfaStack.<span class="hljs-built_in">pop</span>();<br><br>			NFA n = <span class="hljs-built_in">creatNFA</span>(Ans.N_StateNum );		<span class="hljs-comment">/*新建一个NFA*/</span><br>			Ans.N_StateNum += <span class="hljs-number">2</span>;					<span class="hljs-comment">/*NFA状态总数加2*/</span><br><br>			n.head-&gt;E_closure_to.<span class="hljs-built_in">insert</span>(n1.head-&gt;state_id);				<span class="hljs-comment">/*n的头通过ε指向n1的头*/</span><br>			n.head-&gt;E_closure_to.<span class="hljs-built_in">insert</span>(n2.head-&gt;state_id);				<span class="hljs-comment">/*n的头通过ε指向n2的头*/</span><br>			n1.tail-&gt;E_closure_to.<span class="hljs-built_in">insert</span>(n.tail-&gt;state_id);				<span class="hljs-comment">/*n1的尾通过ε指向n的尾*/</span><br>			n2.tail-&gt;E_closure_to.<span class="hljs-built_in">insert</span>(n.tail-&gt;state_id);				<span class="hljs-comment">/*n2的尾通过ε指向n的尾*/</span><br><br>			NfaStack.<span class="hljs-built_in">push</span>(n);					<span class="hljs-comment">/*最后将新生成的NFA入栈*/</span><br>		&#125;<br><br>		<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (s[i] == <span class="hljs-string">&#x27;&amp;&#x27;</span>)		<span class="hljs-comment">/*遇到连接运算符*/</span><br>		&#123;<br><br>			NFA n1, n2, n;				<span class="hljs-comment">/*定义一个新的NFA n*/</span><br><br>			n2 = NfaStack.<span class="hljs-built_in">top</span>();				<span class="hljs-comment">/*从栈中弹出两个NFA，栈顶为n2，次栈顶为n1*/</span><br>			NfaStack.<span class="hljs-built_in">pop</span>();<br><br>			n1 = NfaStack.<span class="hljs-built_in">top</span>();<br>			NfaStack.<span class="hljs-built_in">pop</span>();<br><br>			<span class="hljs-comment">//add(n1.tail, n2.head);				/*n1的尾通过ε指向n2的尾*/</span><br>			n1.tail-&gt;E_closure_to.<span class="hljs-built_in">insert</span>(n2.head-&gt;state_id);<br>			<span class="hljs-comment">/*set&lt;int&gt; t= n1.tail-&gt;E_closure;</span><br><span class="hljs-comment">			t.insert(n2.head-&gt;ID);</span><br><span class="hljs-comment">			n1.tail.E_closure = t;*/</span><br><br><br>			n.head = n1.head;					<span class="hljs-comment">/*n的头为n1的头*/</span><br>			n.tail = n2.tail;					<span class="hljs-comment">/*n的尾为n2的尾*/</span><br><br>			NfaStack.<span class="hljs-built_in">push</span>(n);					<span class="hljs-comment">/*最后将新生成的NFA入栈*/</span><br>		&#125;<br>	&#125;<br><br>	Ans.head = NfaStack.<span class="hljs-built_in">top</span>().head;<br>	Ans.tail = NfaStack.<span class="hljs-built_in">top</span>().tail;<br>	<span class="hljs-keyword">return</span> Ans;		<span class="hljs-comment">/*最后的栈顶元素即为生成好的NFA*/</span><br>&#125;<br><br><span class="hljs-comment">/*打印NFA函数*/</span><br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">printNFA</span><span class="hljs-params">(NFA &amp;n)</span></span><br><span class="hljs-function"></span>&#123;<br><br>	cout &lt;&lt; <span class="hljs-string">&quot;***************     NFA     ***************&quot;</span> &lt;&lt; endl &lt;&lt; endl;<br>	cout &lt;&lt; <span class="hljs-string">&quot;NFA总共有&quot;</span> &lt;&lt; n.N_StateNum &lt;&lt; <span class="hljs-string">&quot;个状态，&quot;</span> &lt;&lt; endl;<br>	cout &lt;&lt; <span class="hljs-string">&quot;初态为&quot;</span> &lt;&lt; n.head-&gt;state_id &lt;&lt; <span class="hljs-string">&quot;，终态为&quot;</span> &lt;&lt; n.tail-&gt;state_id &lt;&lt; <span class="hljs-string">&quot;。&quot;</span> &lt;&lt; endl &lt;&lt; endl &lt;&lt; <span class="hljs-string">&quot;转移函数为：&quot;</span> &lt;&lt; endl;<br><br>	<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; n.N_StateNum; i++)		<span class="hljs-comment">/*遍历NFA状态数组*/</span><br>	&#123;<br><br>		<span class="hljs-keyword">if</span> (NFA_States[i].get_value != <span class="hljs-string">&#x27;#&#x27;</span>)			<span class="hljs-comment">/*如果弧上的值不是初始时的‘#’则输出*/</span><br>		&#123;<br>			cout &lt;&lt; NFA_States[i].state_id &lt;&lt; <span class="hljs-string">&quot;--&gt;&#x27;&quot;</span> &lt;&lt; NFA_States[i].get_value &lt;&lt; <span class="hljs-string">&quot;&#x27;--&gt;&quot;</span> &lt;&lt; NFA_States[i].Trans_to &lt;&lt; <span class="hljs-string">&#x27;\t&#x27;</span>;<br>		&#125;<br><br>		set&lt;<span class="hljs-type">int</span>&gt;::iterator it;					<span class="hljs-comment">/*输出该状态经过ε到达的状态*/</span><br>		<span class="hljs-keyword">for</span> (it = NFA_States[i].E_closure_to.<span class="hljs-built_in">begin</span>(); it != NFA_States[i].E_closure_to.<span class="hljs-built_in">end</span>(); it++)<br>		&#123;<br>			cout &lt;&lt; NFA_States[i].state_id &lt;&lt; <span class="hljs-string">&quot;--&gt;&#x27;&quot;</span> &lt;&lt; <span class="hljs-string">&#x27; &#x27;</span> &lt;&lt; <span class="hljs-string">&quot;&#x27;--&gt;&quot;</span> &lt;&lt; *it &lt;&lt; <span class="hljs-string">&#x27;\t&#x27;</span>;<br>		&#125;<br><br>		cout &lt;&lt; endl;<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>




<ul>
<li><p>定义一个DFA　Ans用于保存最后的答案，接受参数NFA n作为读入数据<br>  Ans的初态为0<br>  初态0的E_closure就等于n的E_closure<br>  判断初态是否为终态<br>  dfa数量加一</p>
</li>
<li><p>定义一个存DFA状态（int类型）的栈：把dfa的初态存入栈，只要栈不为空，就一直循环：<br>  栈顶元素出栈<br>  遍历有穷字母表计算每个字母的ε-cloure(move(ch))<br>  如果求出来的状态集不为空且与之前求出来的状态集不同，则增加一个新DFA状态</p>
<div class="code-wrapper"><pre><code class="hljs">  将新求出来的状态集加入到状态集合中
  该状态弧的输入即为当前终结符
  弧转移到的状态为最后一个DFA状态
  该状态弧的数目加一，更新转移矩阵
  判断是否为终态
  将新的状态号加入栈中
  DFA状态总数加一
</code></pre></div>
<p>  求出来的状态集在之前求出的某个状态集相同，不增加状态，只增加该状态的弧</p>
<div class="code-wrapper"><pre><code class="hljs">  遍历之前求出来的状态集合，找到与该集合相同的DFA状态
  该状态弧的输入即为当前终结符
  该弧转移到的状态即为i
  该状态弧的数目加一，更新转移矩阵，
</code></pre></div>
</li>
<li><p>遍历Ans的所有状态 ，如果该状态是NFA的终态，则将该状态号加入到Ans的终态集中<br>转换结束。   </p>
</li>
<li><p>状态转移矩阵的含义：<br>  状态1通过接收字母b到达状态二，表示为：<br>  Ans.trans[1][(int)b-(int)*All_Char.begin()] == 2；<br>  重点是trans的第二维的意义，由于其是int类型所以只能用ascii码来表示，<br>  故用（int）将char类型强制类型转换成对应的ascii码，<br>  但是由于有穷字母表一般不能包含所有ascii码表，所以会造成一定的空间的浪费（最小的ascii码前面的所有空间都被浪费）<br>  所以我们实际上采用的是某个字符对应有穷字母表内ascii码最小字符的相对位置来判断的<br>  所以字符b在转移矩阵中的位置应该是(int)b-(int)*All_Char.begin()</p>
</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">pragma</span> once</span><br><br><br><br><span class="hljs-comment">/*求一个状态集的ε-cloure*/</span><br><span class="hljs-function">set&lt;<span class="hljs-type">int</span>&gt; <span class="hljs-title">epcloure</span><span class="hljs-params">(set&lt;<span class="hljs-type">int</span>&gt; s, NFA  &amp;n)</span></span><br><span class="hljs-function"></span>&#123;<br><br>	stack&lt;<span class="hljs-type">int</span>&gt; epStack;		<span class="hljs-comment">/*(此处栈和队列均可)*/</span><br><br>	set&lt;<span class="hljs-type">int</span>&gt;::iterator it;<br>	<span class="hljs-keyword">for</span> (it = s.<span class="hljs-built_in">begin</span>(); it != s.<span class="hljs-built_in">end</span>(); it++)<br>		epStack.<span class="hljs-built_in">push</span>(*it);			<span class="hljs-comment">/*将该状态集中的每一个元素都压入栈中*/</span><br><br><br>	<span class="hljs-keyword">while</span> (!epStack.<span class="hljs-built_in">empty</span>())			<span class="hljs-comment">/*只要栈不为空*/</span><br>	&#123;<br>		<span class="hljs-type">int</span> temp = epStack.<span class="hljs-built_in">top</span>();		<span class="hljs-comment">/*从栈中弹出一个元素*/</span><br>		epStack.<span class="hljs-built_in">pop</span>();<br><br>		set&lt;<span class="hljs-type">int</span>&gt;::iterator iter;<br>		<span class="hljs-keyword">for</span> (iter = NFA_States[temp].E_closure_to.<span class="hljs-built_in">begin</span>(); iter != NFA_States[temp].E_closure_to.<span class="hljs-built_in">end</span>(); iter++)<br>		&#123;<br>			<span class="hljs-keyword">if</span> (!s.<span class="hljs-built_in">count</span>(*iter))				<span class="hljs-comment">/*遍历它通过ε能转换到的状态集*/</span><br>			&#123;								<span class="hljs-comment">/*如果当前元素没有在集合中出现*/</span><br>				s.<span class="hljs-built_in">insert</span>(*iter);			<span class="hljs-comment">/*则把它加入集合中*/</span><br>				epStack.<span class="hljs-built_in">push</span>(*iter);		<span class="hljs-comment">/*同时压入栈中*/</span><br>			&#125;<br>		&#125;<br>	&#125;<br><br>	<span class="hljs-keyword">return</span> s;		<span class="hljs-comment">/*最后的s即为ε-cloure*/</span><br>&#125;<br><br><span class="hljs-comment">/*求一个状态集s的ε-cloure(move(ch))*/</span><br><span class="hljs-function">set&lt;<span class="hljs-type">int</span>&gt; <span class="hljs-title">moveEpCloure</span><span class="hljs-params">(set&lt;<span class="hljs-type">int</span>&gt; s, <span class="hljs-type">char</span> ch, NFA &amp;n)</span></span><br><span class="hljs-function"></span>&#123;<br><br>	set&lt;<span class="hljs-type">int</span>&gt; temp;<br><br>	set&lt;<span class="hljs-type">int</span>&gt;::iterator it;<br>	<span class="hljs-keyword">for</span> (it = s.<span class="hljs-built_in">begin</span>(); it != s.<span class="hljs-built_in">end</span>(); it++)		<span class="hljs-comment">/*遍历当前集合s中的每个元素*/</span><br>	&#123;<br>		<span class="hljs-keyword">if</span> (NFA_States[*it].get_value == ch)				<span class="hljs-comment">/*如果对应转换弧上的值为ch*/</span><br>		&#123;<br>			temp.<span class="hljs-built_in">insert</span>(NFA_States[*it].Trans_to);		<span class="hljs-comment">/*则把该弧通过ch转换到的状态加入到集合temp中*/</span><br>		&#125;<br>	&#125;<br><br>	temp = <span class="hljs-built_in">epcloure</span>(temp ,n);			<span class="hljs-comment">/*最后求temp的ε闭包*/</span><br>	<span class="hljs-keyword">return</span> temp;<br>&#125;<br><br><span class="hljs-comment">/*判断一个状态是否为终态*/</span><br><span class="hljs-function"><span class="hljs-type">bool</span> <span class="hljs-title">IsEnd</span><span class="hljs-params">(NFA &amp;n, set&lt;<span class="hljs-type">int</span>&gt; s)</span></span><br><span class="hljs-function"></span>&#123;<br>	set&lt;<span class="hljs-type">int</span>&gt;::iterator it;<br>	<span class="hljs-keyword">for</span> (it = s.<span class="hljs-built_in">begin</span>(); it != s.<span class="hljs-built_in">end</span>(); it++)	<span class="hljs-comment">/*遍历该状态所包含的nfa状态集*/</span><br>		<span class="hljs-keyword">if</span> (*it == n.tail-&gt;state_id)				<span class="hljs-comment">/*如果包含nfa的终态，则该状态为终态，返回true*/</span><br>			<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br><br>	<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;		<span class="hljs-comment">/*如果不包含，则不是终态，返回false*/</span><br>&#125;<br><br><span class="hljs-comment">/*nfa转dfa主函数*/</span><br><span class="hljs-function">DFA <span class="hljs-title">Nfa_To_Dfa</span><span class="hljs-params">(NFA n)</span>		<span class="hljs-comment">/*参数为nfa和后缀表达式*/</span></span><br><span class="hljs-function"></span>&#123;<br><br><br>	cout &lt;&lt; <span class="hljs-string">&quot;***************     DFA     ***************&quot;</span> &lt;&lt; endl &lt;&lt; endl;<br><br>	<span class="hljs-type">int</span> i;<br>	DFA Ans;<span class="hljs-comment">//转化成的DFA（结果）</span><br>	set&lt;set&lt;<span class="hljs-type">int</span>&gt;&gt; states;		<span class="hljs-comment">/*定义一个存储整数集合的集合，用于判断求出一个状态集s的ε-cloure(move(ch))后是否出现新状态*/</span><br>	set&lt;<span class="hljs-type">int</span>&gt; tempSet;<br><br>	Ans.Letter_List = All_Char;<span class="hljs-comment">/*把操作数加入到dfa的有穷字母表中*/</span><br>	Ans.Beg_State = <span class="hljs-number">0</span>;		<span class="hljs-comment">/*dfa的初态为0*/</span><br><br>		<br>	tempSet.<span class="hljs-built_in">insert</span>(n.head-&gt;state_id);		<span class="hljs-comment">/*将nfa的初态加入到集合中*/</span><br>	Ans.DFA_States[<span class="hljs-number">0</span>].E_closure_to = <span class="hljs-built_in">epcloure</span>(tempSet,n);		<span class="hljs-comment">/*求dfa的初态*/</span><br>	Ans.DFA_States[<span class="hljs-number">0</span>].isEnd = <span class="hljs-built_in">IsEnd</span>(n, Ans.DFA_States[<span class="hljs-number">0</span>].E_closure_to);		<span class="hljs-comment">/*判断初态是否为终态*/</span><br><br>	Ans.D_StateNum++;			<span class="hljs-comment">/*dfa数量加一*/</span><br><br>	stack &lt;<span class="hljs-type">int</span>&gt; q;<br>	q.<span class="hljs-built_in">push</span>(Ans.Beg_State);		<span class="hljs-comment">/*把dfa的初态存入栈中*/</span><br><br>	<span class="hljs-keyword">while</span> (!q.<span class="hljs-built_in">empty</span>())		<span class="hljs-comment">/*只要栈不为空，就一直循环*/</span><br>	&#123;<br><br>		<span class="hljs-type">int</span> num = q.<span class="hljs-built_in">top</span>();				<span class="hljs-comment">/*出去栈顶元素*/</span><br>		q.<span class="hljs-built_in">pop</span>();<br><br>		set&lt;<span class="hljs-type">char</span>&gt;::iterator it;<br>		<span class="hljs-keyword">for</span> (it = Ans.Letter_List.<span class="hljs-built_in">begin</span>(); it != Ans.Letter_List.<span class="hljs-built_in">end</span>(); it++)		<span class="hljs-comment">/*遍历有穷字母表*/</span><br>		&#123;<br><br>			set&lt;<span class="hljs-type">int</span>&gt; temp = <span class="hljs-built_in">moveEpCloure</span>(Ans.DFA_States[num].E_closure_to, *it ,n);		<span class="hljs-comment">/*计算每个终结符的ε-cloure(move(ch))*/</span><br><br>			<span class="hljs-keyword">if</span> (!states.<span class="hljs-built_in">count</span>(temp) &amp;&amp; !temp.<span class="hljs-built_in">empty</span>())	<span class="hljs-comment">/*如果求出来的状态集不为空且与之前求出来的状态集不同，则新建一个DFA状态*/</span><br>			&#123;<br><br>				states.<span class="hljs-built_in">insert</span>(temp);				<span class="hljs-comment">/*将新求出来的状态集加入到状态集合中*/</span><br><br>				Ans.DFA_States[Ans.D_StateNum].E_closure_to = temp;<br><br>				Ans.DFA_States[num].Edges[Ans.DFA_States[num].Edge_Num].get_value = *it;				<span class="hljs-comment">/*该状态弧的输入即为当前终结符*/</span><br>				Ans.DFA_States[num].Edges[Ans.DFA_States[num].Edge_Num].Trans_to = Ans.D_StateNum;		<span class="hljs-comment">/*弧转移到的状态为最后一个DFA状态*/</span><br>				Ans.DFA_States[num].Edge_Num++;												<span class="hljs-comment">/*该状态弧的数目加一*/</span><br><br>				<span class="hljs-comment">//d.trans[num][*it - &#x27;a&#x27;] = dfaStateNum;		/*更新转移矩阵*/</span><br>				Ans.trans[num][*it - (<span class="hljs-type">int</span>)*All_Char.<span class="hljs-built_in">begin</span>()] = Ans.D_StateNum;		<span class="hljs-comment">/*更新转移矩阵*/</span><br><br>				Ans.DFA_States[Ans.D_StateNum].isEnd = <span class="hljs-built_in">IsEnd</span>(n, Ans.DFA_States[Ans.D_StateNum].E_closure_to);	<span class="hljs-comment">/*判断是否为终态*/</span><br><br>				q.<span class="hljs-built_in">push</span>(Ans.D_StateNum);		<span class="hljs-comment">/*将新的状态号加入队列中*/</span><br><br>				Ans.D_StateNum++;		<span class="hljs-comment">/*DFA状态总数加一*/</span><br>			&#125;<br>			<span class="hljs-keyword">else</span>			<span class="hljs-comment">/*求出来的状态集在之前求出的某个状态集相同*/</span><br>			&#123;<br>				<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; Ans.D_StateNum; i++)		<span class="hljs-comment">/*遍历之前求出来的状态集合*/</span><br>				&#123;<br>					<span class="hljs-keyword">if</span> (temp == Ans.DFA_States[i].E_closure_to)		<span class="hljs-comment">/*找到与该集合相同的DFA状态*/</span><br>					&#123;<br><br>						Ans.DFA_States[num].Edges[Ans.DFA_States[num].Edge_Num].get_value = *it;		<span class="hljs-comment">/*该状态弧的输入即为当前终结符*/</span><br>						Ans.DFA_States[num].Edges[Ans.DFA_States[num].Edge_Num].Trans_to = i;			<span class="hljs-comment">/*该弧转移到的状态即为i*/</span><br>						Ans.DFA_States[num].Edge_Num++;										<span class="hljs-comment">/*该状态弧的数目加一*/</span><br><br>						<span class="hljs-comment">//d.trans[num][*it - &#x27;a&#x27;] = i;		/*更新转移矩阵*/</span><br>						Ans.trans[num][*it - (<span class="hljs-type">int</span>)*All_Char.<span class="hljs-built_in">begin</span>()] = i;		<span class="hljs-comment">/*更新转移矩阵*/</span><br><br>						<span class="hljs-keyword">break</span>;<br>					&#125;<br>				&#125;<br>			&#125;<br>		&#125;<br>	&#125;<br><br>	<span class="hljs-comment">/*计算dfa的终态集*/</span><br>	<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; Ans.D_StateNum; i++)	<span class="hljs-comment">/*遍历dfa的所有状态*/</span><br>		<span class="hljs-keyword">if</span> (Ans.DFA_States[i].isEnd == <span class="hljs-literal">true</span>)		<span class="hljs-comment">/*如果该状态是终态*/</span><br>			Ans.End_States.<span class="hljs-built_in">insert</span>(i);		<span class="hljs-comment">/*则将该状态号加入到dfa的终态集中*/</span><br>		<br>	<span class="hljs-keyword">return</span> Ans;<br>&#125;<br><br><span class="hljs-comment">/*打印dfa函数*/</span><br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">printDFA</span><span class="hljs-params">(DFA &amp;d)</span></span><br><span class="hljs-function"></span>&#123;<br><br>	<span class="hljs-type">int</span> i, j;<br>	cout &lt;&lt; <span class="hljs-string">&quot;DFA总共有&quot;</span> &lt;&lt; d.D_StateNum &lt;&lt; <span class="hljs-string">&quot;个状态，&quot;</span> &lt;&lt; <span class="hljs-string">&quot;初态为&quot;</span> &lt;&lt; d.Beg_State &lt;&lt; endl &lt;&lt; endl;<br><br>	cout &lt;&lt; <span class="hljs-string">&quot;有穷字母表为｛ &quot;</span>;<br>	set&lt;<span class="hljs-type">char</span>&gt;::iterator it;<br>	<span class="hljs-keyword">for</span> (it = d.Letter_List.<span class="hljs-built_in">begin</span>(); it != d.Letter_List.<span class="hljs-built_in">end</span>(); it++)<br>	&#123;<br>		cout &lt;&lt; *it &lt;&lt; <span class="hljs-string">&#x27; &#x27;</span>;<br>	&#125;<br>	cout &lt;&lt; <span class="hljs-string">&#x27;&#125;&#x27;</span> &lt;&lt; endl &lt;&lt; endl;<br><br>	cout &lt;&lt; <span class="hljs-string">&quot;终态集为｛ &quot;</span>;<br>	set&lt;<span class="hljs-type">int</span>&gt;::iterator iter;<br>	<span class="hljs-keyword">for</span> (iter = d.End_States.<span class="hljs-built_in">begin</span>(); iter != d.End_States.<span class="hljs-built_in">end</span>(); iter++)<br>	&#123;<br>		cout &lt;&lt; *iter &lt;&lt; <span class="hljs-string">&#x27; &#x27;</span>;<br>	&#125;<br>	cout &lt;&lt; <span class="hljs-string">&#x27;&#125;&#x27;</span> &lt;&lt; endl &lt;&lt; endl;<br><br>	cout &lt;&lt; <span class="hljs-string">&quot;转移函数为：&quot;</span> &lt;&lt; endl;<br>	<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; d.D_StateNum; i++)<br>	&#123;<br>		<span class="hljs-keyword">for</span> (j = <span class="hljs-number">0</span>; j &lt; d.DFA_States[i].Edge_Num; j++)<br>		&#123;<br><br>			<span class="hljs-keyword">if</span> (d.DFA_States[d.DFA_States[i].Edges[j].Trans_to].isEnd == <span class="hljs-literal">true</span>)<br>			&#123;<br>				cout &lt;&lt; d.DFA_States[i].state_id &lt;&lt; <span class="hljs-string">&quot;--&gt;&#x27;&quot;</span> &lt;&lt; d.DFA_States[i].Edges[j].get_value;<br>				cout &lt;&lt; <span class="hljs-string">&quot;&#x27;--&gt;&lt;&quot;</span> &lt;&lt; d.DFA_States[i].Edges[j].Trans_to &lt;&lt; <span class="hljs-string">&quot;&gt;\t&quot;</span>;<br>			&#125;<br>			<span class="hljs-keyword">else</span><br>			&#123;<br>				cout &lt;&lt; d.DFA_States[i].state_id &lt;&lt; <span class="hljs-string">&quot;--&gt;&#x27;&quot;</span> &lt;&lt; d.DFA_States[i].Edges[j].get_value;<br>				cout &lt;&lt; <span class="hljs-string">&quot;&#x27;--&gt;&quot;</span> &lt;&lt; d.DFA_States[i].Edges[j].Trans_to &lt;&lt; <span class="hljs-string">&#x27;\t&#x27;</span>;<br>			&#125;<br>		&#125;<br>		cout &lt;&lt; endl;<br>	&#125;<br><br>	cout &lt;&lt; endl &lt;&lt; <span class="hljs-string">&quot;转移矩阵为：&quot;</span> &lt;&lt; endl &lt;&lt; <span class="hljs-string">&quot;     &quot;</span>;<br>	set&lt;<span class="hljs-type">char</span>&gt;::iterator t;<br>	<span class="hljs-keyword">for</span> (t = d.Letter_List.<span class="hljs-built_in">begin</span>(); t != d.Letter_List.<span class="hljs-built_in">end</span>(); t++)<br>	&#123;<br>		cout &lt;&lt; *t &lt;&lt; <span class="hljs-string">&quot;   &quot;</span>;<br>	&#125;<br>	cout &lt;&lt; endl;<br><br>	<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; d.D_StateNum; i++)<br>	&#123;<br><br>		<span class="hljs-keyword">if</span> (d.End_States.<span class="hljs-built_in">count</span>(i))<br>		&#123;<br>			cout &lt;&lt; <span class="hljs-string">&#x27;&lt;&#x27;</span> &lt;&lt; i &lt;&lt; <span class="hljs-string">&quot;&gt;  &quot;</span>;<br>		&#125;<br>		<span class="hljs-keyword">else</span><br>		&#123;<br>			cout &lt;&lt; <span class="hljs-string">&#x27; &#x27;</span> &lt;&lt; i &lt;&lt; <span class="hljs-string">&quot;   &quot;</span>;<br>		&#125;<br><br>		<span class="hljs-type">int</span> n = (<span class="hljs-type">int</span>)*All_Char.<span class="hljs-built_in">begin</span>();<br>		<span class="hljs-type">int</span> m = (<span class="hljs-type">int</span>)*(--All_Char.<span class="hljs-built_in">end</span>());<br><br>		<span class="hljs-keyword">for</span> (j = <span class="hljs-number">0</span>; j &lt; m + <span class="hljs-number">2</span>; j++)<br>			<span class="hljs-comment">//for (j = 0; j &lt; 26; j++)</span><br>		&#123;<br>			<span class="hljs-comment">//if (d.terminator.count(j + &#x27;a&#x27;))</span><br>			<span class="hljs-keyword">if</span> (d.Letter_List.<span class="hljs-built_in">count</span>(j + n))<br>			&#123;<br>				<span class="hljs-keyword">if</span> (d.trans[i][j] != <span class="hljs-number">-1</span>)<br>				&#123;<br>					cout &lt;&lt; d.trans[i][j] &lt;&lt; <span class="hljs-string">&quot;   &quot;</span>;<br>				&#125;<br>				<span class="hljs-keyword">else</span><br>				&#123;<br>					cout &lt;&lt; <span class="hljs-string">&quot;    &quot;</span>;<br>				&#125;<br>			&#125;<br>		&#125;<br><br>		cout &lt;&lt; endl;<br>	&#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>
<p>DFA最小化：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">pragma</span> once</span><br><br>set&lt;<span class="hljs-type">int</span>&gt; s[MAX];					<span class="hljs-comment">/*划分出来的集合数组*/</span><br><br><span class="hljs-keyword">struct</span> <span class="hljs-title class_">stateSet</span>			<span class="hljs-comment">/*划分状态集*/</span><br>&#123;<br>	<span class="hljs-type">int</span> index;			<span class="hljs-comment">/*该状态集所能转换到的状态集标号*/</span><br>	set&lt;<span class="hljs-type">int</span>&gt; s;			<span class="hljs-comment">/*该状态集中的dfa状态号*/</span><br>&#125;;<br><br><span class="hljs-comment">//***********************************************************************************************************</span><br><br><br><span class="hljs-comment">/*当前划分总数为count，返回状态n所属的状态集标号i*/</span><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">findSetNum</span><span class="hljs-params">(<span class="hljs-type">int</span> count, <span class="hljs-type">int</span> n)</span></span><br><span class="hljs-function"></span>&#123;<br>	<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; count; i++)<br>		<span class="hljs-keyword">if</span> (s[i].<span class="hljs-built_in">count</span>(n))<br>			<span class="hljs-keyword">return</span> i;<br><br>&#125;<br><br><span class="hljs-comment">/*最小化DFA*/</span><br><span class="hljs-function">DFA <span class="hljs-title">min_DFA</span><span class="hljs-params">(DFA &amp;d)</span></span><br><span class="hljs-function"></span>&#123;<br><br>	<span class="hljs-type">int</span> i, j;<br>	cout &lt;&lt; endl &lt;&lt; <span class="hljs-string">&quot;*************     minDFA     **************&quot;</span> &lt;&lt; endl &lt;&lt; endl;<br><br>	DFA minDfa;<br>	minDfa.Letter_List = d.Letter_List;		<span class="hljs-comment">/*把dfa的终结符集赋给minDfa*/</span><br><br>	<span class="hljs-built_in">memset</span>(minDfa.trans, <span class="hljs-number">-1</span>, <span class="hljs-built_in">sizeof</span>(minDfa.trans));		<span class="hljs-comment">/*初始化minDfa转移矩阵*/</span><br><br>	<span class="hljs-comment">/*做第一次划分，即将终态与非终态分开*/</span><br>	<span class="hljs-type">bool</span> endFlag = <span class="hljs-literal">true</span>;					<span class="hljs-comment">/*判断dfa的所有状态是否全为终态的标志*/</span><br>	<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; d.D_StateNum; i++)	<span class="hljs-comment">/*遍历dfa状态数组*/</span><br>	&#123;<br>		<span class="hljs-keyword">if</span> (d.DFA_States[i].isEnd == <span class="hljs-literal">false</span>)			<span class="hljs-comment">/*如果该dfa状态不是终态*/</span><br>		&#123;<br><br>			endFlag = <span class="hljs-literal">false</span>;						<span class="hljs-comment">/*标志应为false*/</span><br>			minDfa.D_StateNum = <span class="hljs-number">2</span>;						<span class="hljs-comment">/*第一次划分应该有两个集合*/</span><br><br>			s[<span class="hljs-number">1</span>].<span class="hljs-built_in">insert</span>(d.DFA_States[i].state_id);		<span class="hljs-comment">/*把该状态的状态号加入s[1]集合中*/</span><br>		&#125;<br>		<span class="hljs-keyword">else</span>									<span class="hljs-comment">/*如果该dfa状态是终态*/</span><br>		&#123;<br>			s[<span class="hljs-number">0</span>].<span class="hljs-built_in">insert</span>(d.DFA_States[i].state_id);		<span class="hljs-comment">/*把该状态的状态号加入s[0]集合中*/</span><br>		&#125;<br>	&#125;<br><br>	<span class="hljs-keyword">if</span> (endFlag)					<span class="hljs-comment">/*如果标志为真，则所有dfa状态都是终态*/</span><br>		minDfa.D_StateNum = <span class="hljs-number">1</span>;			<span class="hljs-comment">/*第一次划分结束应只有一个集合*/</span><br><br><br>	<span class="hljs-type">bool</span> cutFlag = <span class="hljs-literal">true</span>;		<span class="hljs-comment">/*上一次是否产生新的划分的标志*/</span><br>	<span class="hljs-keyword">while</span> (cutFlag)				<span class="hljs-comment">/*只要上一次产生新的划分就继续循环*/</span><br>	&#123;<br><br>		<span class="hljs-type">int</span> cutCount = <span class="hljs-number">0</span>;			<span class="hljs-comment">/*需要产生新的划分的数量*/</span><br>		<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; minDfa.D_StateNum; i++)			<span class="hljs-comment">/*遍历每个划分集合*/</span><br>		&#123;<br><br>			set&lt;<span class="hljs-type">char</span>&gt;::iterator it;<br>			<span class="hljs-keyword">for</span> (it = d.Letter_List.<span class="hljs-built_in">begin</span>(); it != d.Letter_List.<span class="hljs-built_in">end</span>(); it++)		<span class="hljs-comment">/*遍历dfa的终结符集*/</span><br>			&#123;<br><br>				<span class="hljs-type">int</span> setNum = <span class="hljs-number">0</span>;				<span class="hljs-comment">/*当前缓冲区中的状态集个数*/</span><br>				stateSet temp[<span class="hljs-number">20</span>];			<span class="hljs-comment">/*划分状态集“缓冲区”*/</span><br><br>				set&lt;<span class="hljs-type">int</span>&gt;::iterator iter;<br>				<span class="hljs-keyword">for</span> (iter = s[i].<span class="hljs-built_in">begin</span>(); iter != s[i].<span class="hljs-built_in">end</span>(); iter++)		<span class="hljs-comment">/*遍历集合中的每个状态号*/</span><br>				&#123;<br><br>					<span class="hljs-type">bool</span> epFlag = <span class="hljs-literal">true</span>;			<span class="hljs-comment">/*判断该集合中是否存在没有该终结符对应的转换弧的状态*/</span><br>					<span class="hljs-keyword">for</span> (j = <span class="hljs-number">0</span>; j &lt; d.DFA_States[*iter].Edge_Num; j++)		<span class="hljs-comment">/*遍历该状态的所有边*/</span><br>					&#123;<br><br>						<span class="hljs-keyword">if</span> (d.DFA_States[*iter].Edges[j].get_value == *it)		<span class="hljs-comment">/*如果该边的输入为该终结符*/</span><br>						&#123;<br><br>							epFlag = <span class="hljs-literal">false</span>;			<span class="hljs-comment">/*则标志为false*/</span><br><br>							<span class="hljs-comment">/*计算该状态转换到的状态集的标号*/</span><br>							<span class="hljs-type">int</span> transNum = <span class="hljs-built_in">findSetNum</span>(minDfa.D_StateNum, d.DFA_States[*iter].Edges[j].Trans_to);<br><br>							<span class="hljs-type">int</span> curSetNum = <span class="hljs-number">0</span>;			<span class="hljs-comment">/*遍历缓冲区，寻找是否存在到达这个标号的状态集*/</span><br>							<span class="hljs-keyword">while</span> ((temp[curSetNum].index != transNum) &amp;&amp; (curSetNum &lt; setNum))<br>							&#123;<br>								curSetNum++;<br>							&#125;<br><br>							<span class="hljs-keyword">if</span> (curSetNum == setNum)		<span class="hljs-comment">/*缓冲区中不存在到达这个标号的状态集*/</span><br>							&#123;<br><br>								<span class="hljs-comment">/*在缓冲区中新建一个状态集*/</span><br>								temp[setNum].index = transNum;		<span class="hljs-comment">/*该状态集所能转换到的状态集标号为transNum*/</span><br>								temp[setNum].s.<span class="hljs-built_in">insert</span>(*iter);		<span class="hljs-comment">/*把当前状态添加到该状态集中*/</span><br><br>								setNum++;		<span class="hljs-comment">/*缓冲区中的状态集个数加一*/</span><br>							&#125;<br>							<span class="hljs-keyword">else</span>			<span class="hljs-comment">/*缓冲区中存在到达这个标号的状态集*/</span><br>							&#123;<br>								temp[curSetNum].s.<span class="hljs-built_in">insert</span>(*iter);	<span class="hljs-comment">/*把当前状态加入到该状态集中*/</span><br>							&#125;<br>						&#125;<br>					&#125;<br><br>					<span class="hljs-keyword">if</span> (epFlag)		<span class="hljs-comment">/*如果该状态不存在与该终结符对应的转换弧*/</span><br>					&#123;<br><br>						<span class="hljs-comment">/*寻找缓冲区中是否存在转换到标号为-1的状态集</span><br><span class="hljs-comment">						这里规定如果不存在转换弧，则它所到达的状态集标号为-1*/</span><br>						<span class="hljs-type">int</span> curSetNum = <span class="hljs-number">0</span>;<br>						<span class="hljs-keyword">while</span> ((temp[curSetNum].index != <span class="hljs-number">-1</span>) &amp;&amp; (curSetNum &lt; setNum))<br>						&#123;<br>							curSetNum++;<br>						&#125;<br><br>						<span class="hljs-keyword">if</span> (curSetNum == setNum)			<span class="hljs-comment">/*如果不存在这样的状态集*/</span><br>						&#123;<br><br>							<span class="hljs-comment">/*在缓冲区中新建一个状态集*/</span><br>							temp[setNum].index = <span class="hljs-number">-1</span>;			<span class="hljs-comment">/*该状态集转移到的状态集标号为-1*/</span><br>							temp[setNum].s.<span class="hljs-built_in">insert</span>(*iter);		<span class="hljs-comment">/*把当前状态加入到该状态集中*/</span><br><br>							setNum++;		<span class="hljs-comment">/*缓冲区中的状态集个数加一*/</span><br>						&#125;<br>						<span class="hljs-keyword">else</span>			<span class="hljs-comment">/*缓冲区中存在到达这个标号的状态集*/</span><br>						&#123;<br>							temp[curSetNum].s.<span class="hljs-built_in">insert</span>(*iter);	<span class="hljs-comment">/*把当前状态加入到该状态集中*/</span><br>						&#125;<br>					&#125;<br>				&#125;<br><br>				<span class="hljs-keyword">if</span> (setNum &gt; <span class="hljs-number">1</span>)	<span class="hljs-comment">/*如果缓冲区中的状态集个数大于1，表示同一个状态集中的元素能转换到不同的状态集，则需要划分*/</span><br>				&#123;<br><br>					cutCount++;		<span class="hljs-comment">/*划分次数加一*/</span><br><br>					<span class="hljs-comment">/*为每组划分创建新的dfa状态*/</span><br>					<span class="hljs-keyword">for</span> (j = <span class="hljs-number">1</span>; j &lt; setNum; j++)		<span class="hljs-comment">/*遍历缓冲区，这里从1开始是将第0组划分留在原集合中*/</span><br>					&#123;<br><br>						set&lt;<span class="hljs-type">int</span>&gt;::iterator t;<br>						<span class="hljs-keyword">for</span> (t = temp[j].s.<span class="hljs-built_in">begin</span>(); t != temp[j].s.<span class="hljs-built_in">end</span>(); t++)<br>						&#123;<br><br>							s[i].<span class="hljs-built_in">erase</span>(*t);						<span class="hljs-comment">/*在原来的状态集中删除该状态*/</span><br>							s[minDfa.D_StateNum].<span class="hljs-built_in">insert</span>(*t);		<span class="hljs-comment">/*在新的状态集中加入该状态*/</span><br>						&#125;<br><br>						minDfa.D_StateNum++;		<span class="hljs-comment">/*最小化DFA状态总数加一*/</span><br>					&#125;<br>				&#125;<br>			&#125;<br>		&#125;<br><br>		<span class="hljs-keyword">if</span> (cutCount == <span class="hljs-number">0</span>)		<span class="hljs-comment">/*如果需要划分的次数为0，表示本次不需要进行划分*/</span><br>		&#123;<br>			cutFlag = <span class="hljs-literal">false</span>;<br>		&#125;<br>	&#125;<br><br>	<span class="hljs-comment">/*遍历每个划分好的状态集*/</span><br>	<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; minDfa.D_StateNum; i++)<br>	&#123;<br><br>		set&lt;<span class="hljs-type">int</span>&gt;::iterator y;<br>		<span class="hljs-keyword">for</span> (y = s[i].<span class="hljs-built_in">begin</span>(); y != s[i].<span class="hljs-built_in">end</span>(); y++)		<span class="hljs-comment">/*遍历集合中的每个元素*/</span><br>		&#123;<br><br>			<span class="hljs-keyword">if</span> (*y == d.Beg_State)			<span class="hljs-comment">/*如果当前状态为dfa的初态，则该最小化DFA状态也为初态*/</span><br>			&#123;<br>				minDfa.Beg_State = i;<br>			&#125;<br><br>			<span class="hljs-keyword">if</span> (d.End_States.<span class="hljs-built_in">count</span>(*y))		<span class="hljs-comment">/*如果当前状态是终态，则该最小化DFA状态也为终态*/</span><br>			&#123;<br><br>				minDfa.DFA_States[i].isEnd = <span class="hljs-literal">true</span>;<br>				minDfa.End_States.<span class="hljs-built_in">insert</span>(i);		<span class="hljs-comment">/*将该最小化DFA状态加入终态集中*/</span><br>			&#125;<br><br>			<span class="hljs-keyword">for</span> (j = <span class="hljs-number">0</span>; j &lt; d.DFA_States[*y].Edge_Num; j++)		<span class="hljs-comment">/*遍历该DFA状态的每条弧，为最小化DFA创建弧*/</span><br>			&#123;<br><br>				<span class="hljs-comment">/*遍历划分好的状态集合，找出该弧转移到的状态现在属于哪个集合*/</span><br>				<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> t = <span class="hljs-number">0</span>; t &lt; minDfa.D_StateNum; t++)<br>				&#123;<br>					<span class="hljs-keyword">if</span> (s[t].<span class="hljs-built_in">count</span>(d.DFA_States[*y].Edges[j].Trans_to))<br>					&#123;<br><br>						<span class="hljs-type">bool</span> haveEdge = <span class="hljs-literal">false</span>;		<span class="hljs-comment">/*判断该弧是否已经创建的标志*/</span><br>						<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> l = <span class="hljs-number">0</span>; l &lt; minDfa.DFA_States[i].Edge_Num; l++)	<span class="hljs-comment">/*遍历已创建的弧*/</span><br>						&#123;					<span class="hljs-comment">/*如果该弧已经存在*/</span><br>							<span class="hljs-keyword">if</span> ((minDfa.DFA_States[i].Edges[l].get_value == d.DFA_States[*y].Edges[j].get_value) &amp;&amp; (minDfa.DFA_States[i].Edges[l].Trans_to == t))<br>							&#123;<br>								haveEdge = <span class="hljs-literal">true</span>;		<span class="hljs-comment">/*标志为真*/</span><br>							&#125;<br>						&#125;<br><br>						<span class="hljs-keyword">if</span> (!haveEdge)		<span class="hljs-comment">/*如果该弧不存在，则创建一条新的弧*/</span><br>						&#123;<br><br>							minDfa.DFA_States[i].Edges[minDfa.DFA_States[i].Edge_Num].get_value = d.DFA_States[*y].Edges[j].get_value;	<span class="hljs-comment">/*弧的值与DFA的相同*/</span><br>							minDfa.DFA_States[i].Edges[minDfa.DFA_States[i].Edge_Num].Trans_to = t;	<span class="hljs-comment">/*该弧转移到的状态为这个状态集的标号*/</span><br><br>							<span class="hljs-comment">//minDfa.trans[i][DfaStates[*y].Edges[j].input - &#x27;a&#x27;] = t;	/*更新转移矩阵*/</span><br>							minDfa.trans[i][d.DFA_States[*y].Edges[j].get_value - (<span class="hljs-type">int</span>)*All_Char.<span class="hljs-built_in">begin</span>()] = t;	<span class="hljs-comment">/*更新转移矩阵*/</span><br><br>							minDfa.DFA_States[i].Edge_Num++;		<span class="hljs-comment">/*该状态的弧的数目加一*/</span><br>						&#125;<br><br>						<span class="hljs-keyword">break</span>;<br>					&#125;<br>				&#125;<br>			&#125;<br>		&#125;<br>	&#125;<br><br>	<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; minDfa.D_StateNum; i++)<br>		<span class="hljs-keyword">for</span> (j = <span class="hljs-number">0</span>; j &lt; minDfa.DFA_States[i].Edge_Num; j++) &#123;<br>			<span class="hljs-keyword">if</span> (minDfa.DFA_States[i].state_id == minDfa.Beg_State)<br>				Beg.<span class="hljs-built_in">insert</span>(minDfa.DFA_States[i].Edges[j].get_value);<span class="hljs-comment">//构造首字母表</span><br>			<span class="hljs-keyword">if</span> (minDfa.End_States.<span class="hljs-built_in">find</span>(minDfa.DFA_States[i].Edges[j].Trans_to) != minDfa.End_States.<span class="hljs-built_in">end</span>())<br>				End.<span class="hljs-built_in">insert</span>(minDfa.DFA_States[i].Edges[j].get_value);<span class="hljs-comment">//构造尾字母表</span><br>		&#125;<br>			<br><br>	<span class="hljs-keyword">return</span> minDfa;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">printMinDFA</span><span class="hljs-params">(DFA d)</span></span><br><span class="hljs-function"></span>&#123;<br><br>	<span class="hljs-type">int</span> i, j;<br>	cout &lt;&lt; <span class="hljs-string">&quot;minDFA总共有&quot;</span> &lt;&lt; d.D_StateNum &lt;&lt; <span class="hljs-string">&quot;个状态，&quot;</span> &lt;&lt; <span class="hljs-string">&quot;初态为&quot;</span> &lt;&lt; d.Beg_State &lt;&lt; endl &lt;&lt; endl;<br><br>	cout &lt;&lt; <span class="hljs-string">&quot;有穷字母表为｛ &quot;</span>;<br>	set&lt;<span class="hljs-type">char</span>&gt;::iterator it;<br>	<span class="hljs-keyword">for</span> (it = d.Letter_List.<span class="hljs-built_in">begin</span>(); it != d.Letter_List.<span class="hljs-built_in">end</span>(); it++)<br>	&#123;<br>		cout &lt;&lt; *it &lt;&lt; <span class="hljs-string">&#x27; &#x27;</span>;<br>	&#125;<br>	cout &lt;&lt; <span class="hljs-string">&#x27;&#125;&#x27;</span> &lt;&lt; endl &lt;&lt; endl;<br><br>	cout &lt;&lt; <span class="hljs-string">&quot;终态集为｛ &quot;</span>;<br>	set&lt;<span class="hljs-type">int</span>&gt;::iterator iter;<br>	<span class="hljs-keyword">for</span> (iter = d.End_States.<span class="hljs-built_in">begin</span>(); iter != d.End_States.<span class="hljs-built_in">end</span>(); iter++)<br>	&#123;<br>		cout &lt;&lt; *iter &lt;&lt; <span class="hljs-string">&#x27; &#x27;</span>;<br>	&#125;<br>	cout &lt;&lt; <span class="hljs-string">&#x27;&#125;&#x27;</span> &lt;&lt; endl &lt;&lt; endl;<br><br>	cout &lt;&lt; <span class="hljs-string">&quot;转移函数为：&quot;</span> &lt;&lt; endl;<br>	<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; d.D_StateNum; i++)<br>	&#123;<br>		<span class="hljs-keyword">for</span> (j = <span class="hljs-number">0</span>; j &lt; d.DFA_States[i].Edge_Num; j++)<br>		&#123;<br><br>			<span class="hljs-keyword">if</span> (d.DFA_States[d.DFA_States[i].Edges[j].Trans_to].isEnd == <span class="hljs-literal">true</span>)<br>			&#123;<br>				cout &lt;&lt; d.DFA_States[i].state_id &lt;&lt; <span class="hljs-string">&quot;--&gt;&#x27;&quot;</span> &lt;&lt; d.DFA_States[i].Edges[j].get_value;<br>				cout &lt;&lt; <span class="hljs-string">&quot;&#x27;--&gt;&lt;&quot;</span> &lt;&lt; d.DFA_States[i].Edges[j].Trans_to &lt;&lt; <span class="hljs-string">&quot;&gt;\t&quot;</span>;<br>			&#125;<br>			<span class="hljs-keyword">else</span><br>			&#123;<br>				cout &lt;&lt; d.DFA_States[i].state_id &lt;&lt; <span class="hljs-string">&quot;--&gt;&#x27;&quot;</span> &lt;&lt; d.DFA_States[i].Edges[j].get_value;<br>				cout &lt;&lt; <span class="hljs-string">&quot;&#x27;--&gt;&quot;</span> &lt;&lt; d.DFA_States[i].Edges[j].Trans_to &lt;&lt; <span class="hljs-string">&#x27;\t&#x27;</span>;<br>			&#125;<br>			<span class="hljs-comment">//if (minDfaStates[i].index == d.startState)</span><br>			<span class="hljs-comment">//	Beg.insert(minDfaStates[i].Edges[j].input);			</span><br>			<br>		&#125;<br>		cout &lt;&lt; endl;<br>	&#125;<br><br>	cout &lt;&lt; endl &lt;&lt; <span class="hljs-string">&quot;转移矩阵为：&quot;</span> &lt;&lt; endl &lt;&lt; <span class="hljs-string">&quot;     &quot;</span>;<br>	set&lt;<span class="hljs-type">char</span>&gt;::iterator t;<br>	<span class="hljs-keyword">for</span> (t = d.Letter_List.<span class="hljs-built_in">begin</span>(); t != d.Letter_List.<span class="hljs-built_in">end</span>(); t++)<br>	&#123;<br>		cout &lt;&lt; *t &lt;&lt; <span class="hljs-string">&quot;   &quot;</span>;<br>	&#125;<br>	cout &lt;&lt; endl;<br><br>	<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; d.D_StateNum; i++)<br>	&#123;<br><br>		<span class="hljs-keyword">if</span> (d.End_States.<span class="hljs-built_in">count</span>(i))<br>		&#123;<br>			cout &lt;&lt; <span class="hljs-string">&#x27;&lt;&#x27;</span> &lt;&lt; i &lt;&lt; <span class="hljs-string">&quot;&gt;  &quot;</span>;<br>		&#125;<br>		<span class="hljs-keyword">else</span><br>		&#123;<br>			cout &lt;&lt; <span class="hljs-string">&#x27; &#x27;</span> &lt;&lt; i &lt;&lt; <span class="hljs-string">&quot;   &quot;</span>;<br>		&#125;<br><br>		<span class="hljs-type">int</span> n = (<span class="hljs-type">int</span>)*All_Char.<span class="hljs-built_in">begin</span>();<br>		<span class="hljs-type">int</span> m = (<span class="hljs-type">int</span>)*(--All_Char.<span class="hljs-built_in">end</span>());<br><br>		<span class="hljs-keyword">for</span> (j = <span class="hljs-number">0</span>; j &lt; m + <span class="hljs-number">2</span>; j++)<br>			<span class="hljs-comment">//for (j = 0; j &lt; 26; j++)</span><br>		&#123;<br>			<span class="hljs-comment">//if (d.terminator.count(j + &#x27;a&#x27;))</span><br>			<span class="hljs-keyword">if</span> (d.Letter_List.<span class="hljs-built_in">count</span>(j + n))			<br>				<span class="hljs-keyword">if</span> (d.trans[i][j] != <span class="hljs-number">-1</span>)				<br>					cout &lt;&lt; d.trans[i][j] &lt;&lt; <span class="hljs-string">&quot;   &quot;</span>;				<br>				<span class="hljs-keyword">else</span>				<br>					cout &lt;&lt; <span class="hljs-string">&quot;    &quot;</span>;<br>		<br>		&#125;<br><br>		cout &lt;&lt; endl;<br>	&#125;<br>	cout &lt;&lt; endl &lt;&lt; <span class="hljs-string">&quot;*******************************************&quot;</span>;<br>&#125;<br><br></code></pre></td></tr></table></figure>
<p>得到由正则表达式转换而来的最小DFA之后，我们就可以利用这个DFA来识别我们的源程序，具体已经在前一篇文章中给出。<br><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_46291251/article/details/115861747">基于DFA的词法分析（一）：程序框架及数据结构</a></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/">计算机基础</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/C/">C++</a>
                    
                      <a class="hover-with-bg" href="/tags/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/">编译原理</a>
                    
                      <a class="hover-with-bg" href="/tags/DFA/">DFA</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/05/01/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__%E9%93%B6%E8%A1%8C%E5%AE%B6%E7%AE%97%E6%B3%95/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">银行家算法</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/04/19/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86__%E5%9F%BA%E4%BA%8EDFA%E7%9A%84%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90%EF%BC%88%E4%B8%80%EF%BC%89%EF%BC%9A%E7%A8%8B%E5%BA%8F%E6%A1%86%E6%9E%B6%E5%8F%8A%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">
                        <span class="hidden-mobile">基于DFA的词法分析（一）：程序框架及数据结构</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://cheney822.gitee.io/" target="_blank" rel="nofollow noopener"><span>备用网址</span></a> 
  </div>
  

  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        皖ICP备2022002876号-1
      </a>
    </span>
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
